<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="UTF-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript" charset="UTF-8"></script>
    <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript" charset="UTF-8"></script>

    <style>
        *{
            margin: 0px;
            padding: 0px;
        }

        html, body{
            width: 100%;
            height: 100%;
        }

        .containers .navbar{
            width: 100%;
            height: 50px;
            /*background-color: #242424;*/
            background-color: #f8f9fa;
            overflow: hidden;
        }

        .containers .navbar a{
            display: inline-block;
            width: 80px;
            color: #212529;
            /*color: #f0f0f0;*/
            font-size: large;
            line-height: 50px;
            text-decoration: none;
        }

        .containers .navbar a:hover{
            background-color: rgba(144, 238, 144, 0.5);
        }

        .containers .navbar .login{
            float: right;
        }

        .containers .part1{
            width: 100%;
            height: 600px;
            overflow: hidden;
        }

        .containers .part1 .left_desc{
            width: 50%;
            height: 600px;
            float: left;
            overflow: scroll;
        }

        .containers .part1 .left_desc h3{
            padding-top: 10px;
            padding-left: 10px;
        }

        .containers .part1 .left_desc pre{
            padding-top: 10px;
            padding-left: 10px;
            font-size: medium;
            font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
        }

        .containers .part1 .right_code{
            width: 50%;
            height: 600px;
            float: right;
        }

        #code{
            width: 100%;
            height: 600px;
        } 

        .containers .part2{
            width: 100%;
            overflow: hidden;
        }

        .containers .part2 .result{
            width: 300px;
            float: left;
        }

        .containers .part2 .btn-submit{
            width: 120px;
            height: 50px;
            font-size: large;
            float: right;
            background-color: #26bb9c;
            color: #fff;
            border-radius: 1pc;
            border: #26bb9c solid 0px;
            margin-top: 10px;
            margin-right: 10px;
        }

        .containers .part2 .btn-submit:hover
        {
            background-color: rgba(38, 187, 156, 0.7); /* 0.5 是透明度，范围 0-1 */
        }

        .containers .part2 .result{
            margin-top: 15px;
            margin-left: 15px;
        }

        .containers .part2 .result pre{
            font-size: large;
        }

    </style>

</head>
<body>
    <div class="containers">
        <!-- 导航栏 -->
        <div class="navbar">
            <a href="/index.html">首页</a>
            <a href="/all_questions">题库</a>
            <a href="">竞赛</a>
            <a href="">讨论</a>
            <a href="">求职</a>
            <a href="" class="login">登录</a>
        </div>
        <div class="part1">
            <div class="left_desc">
                <h3><span id="number">{{number}}</span>.{{title}}.{{star}}</h3>
                <pre>{{desc}}</pre>
            </div>
            <div class="right_code">
                <pre id="code" class="ace_editor" ><textarea class="ace_text-input">{{pre_code}}</textarea></pre>
            </div>
        </div>
        <div class="part2">
            <div class="result"></div>
            <button class="btn-submit" onclick="submit()">提交代码</button>
        </div>

    </div>
    

    <script>
        // 初始化对象
        editor = ace.edit("code");

        // 设置风格和语言
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");

        // 字体⼤⼩
        editor.setFontSize(16);
        // 设置默认制表符的⼤⼩:
        editor.getSession().setTabSize(4);
        // 设置只读（true时只读，⽤于展⽰代码）
        editor.setReadOnly(false);
        // 启⽤提⽰菜单
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        function submit(){
            // 1. 获取有关数据 1.题号 2.代码
            var number = $(".containers .part1 .left_desc h3 #number").text();
            //console.log(number);

            var code = editor.getSession().getValue();
            console.log(code);

            var judge_url = "/judge/" + number;
            console.log(judge_url);

            // 2. 构建json，并向后台发起请求
            $.ajax({
                method: 'Post',
                url: judge_url,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    'code': code,
                    'input': ''
                }),
                success: function(data){
                    // 成功得到结果
                    //console.log(data);
                    show_result(data);
                }
            });
            // 3. 得到结果，解析并显示到 result 中
            function show_result(data)
            {
                // 获取result结果标签
                var result_div = $(".containers .part2 .result");
                result_div.empty();

                // 获取状态码和原因结果
                var _status = data.status;
                var _reason = data.reason;
                var reason_label = $("<p>", {
                        text:_reason
                });
                reason_label.appendTo(result_div);
                if (status == 0) {

                    var _stdout = data.stdout;
                    var _stderr = data.stderr;

                    var stdout_label = $("<pre>", {
                        text:_stdout
                    });

                    var stderr_label = $("<pre>", {
                        text:_stderr
                    });
                    stdout_label.appendTo(result_div);
                    stderr_label.appendTo(result_div);
                }
                else {
                    // 编译运行出错
                }
                
            }
        }
    </script>
</body>
</html>